package com.weiyuwqj.api.model.convert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by huzj on 2016/12/16.
 */
public class UserAgeConvert extends DefaultConvert {
    @Override
    public Object convert(Object val) {
        if (val == null) return "";
        if (!(val instanceof String)) {
            return "";
        }
        String type = val.toString();
        if(type.isEmpty()){
            return "";
        }
        if(type.length() !=15 && type.length() !=18){
            return  "";
        }
        Date birthdate = null;
        if(type.length() ==15){
            String birthday = type.substring(6,12);
            try {
                birthdate = new SimpleDateFormat("yyMMdd").parse(birthday);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }if(type.length() ==18){
            String birthday = type.substring(6,14);
            try {
                birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        return Integer.parseInt(sdf.format(new Date()))-Integer.parseInt(sdf.format(birthdate));
    }
}